
uniform vec4 inputColor;
uniform float threshold;

varying vec2 textureCoordinate;
uniform sampler2D videoFrame;
uniform vec2 tc_offset[9];


void main()
{

     vec4 sample[9];

    for (int i = 0; i < 9; i++)
    {
        sample[i] = texture2D(videoFrame, 
                              gl_TexCoord[0].st + tc_offset[i]);
    }
	
//   1 2 1
//   2 1 2   / 13
//   1 2 1

	float weight = 1.0;

    vec4 color = (weight*sample[0] + (weight*2.0*sample[1]) + weight*sample[2] + 
                    (weight*2.0*sample[3]) + weight*sample[4] + (weight*2.0*sample[5]) + 
                    weight*sample[6] + (weight*2.0*sample[7]) + weight*sample[8]) /13.0;

   //vec4 color = texture2D(videoFrame, gl_TexCoord[0].st);
   vec4 fOut = vec4(0,0,0,0); 
	
   //if ( color.r > 0.7 && color.g > 0.7 && color.b < 0.60 ) //
   
   
   //if ( color.r >= inputColor.r-threshold && color.g >= inputColor.g-threshold && color.b <= inputColor.b )
   
   if ( color.r >= 0.48 && color.r <= 1.0 )
   {
	if ( color.g >= 0.34 && color.g <= 0.5 )
	{
		if ( color.b >= 0.35 && color.b <= 0.5 )
		{
			fOut = vec4(gl_TexCoord[0].s,gl_TexCoord[0].t,1,1); 
		}
	}
   }
   
   //if ( color.r > 0.65 && color.g > 0.65 && color.b < 0.45 ) // nesten perfekt for gul
   
   //if ( color.r > 0.34 && color.r < 0.5 && color.g > 0.38 && color.b < 0.5 && color.b < 0.33 && color.b < 0.35 )
   
  // if ( color.r < 0.2 && color.g < 0.2 && color.b < 0.02 ) 
   
   if ( color.r > 0.65 && color.g > 0.65 && color.b < 0.45 ) 
   {
	fOut = vec4(gl_TexCoord[0].s,gl_TexCoord[0].t,1,1);  //output blue when area to track
   }else
   {
	fOut = vec4(0,0,0,1); // output zero when no area to track
   }

	
   gl_FragColor = fOut; 
   
   //gl_FragColor = texture2D(videoFrame, gl_TexCoord[0].st );
}


/*
void main()
 {
     vec4 sample[9];

    for (int i = 0; i < 9; i++)
    {
        sample[i] = texture2D(videoFrame, 
                              gl_TexCoord[0].st + tc_offset[i]);
    }

//   1 1 1
//   1 1 1   / 13
//   1 1 1

	float weight = 1.0;

    vec4 sumColor = (weight*sample[0] + (weight*1.0*sample[1]) + weight*sample[2] + 
                    (weight*1.0*sample[3]) + weight*sample[4] + (weight*1.0*sample[5]) + 
                    weight*sample[6] + (weight*1.0*sample[7]) + weight*sample[8]) /13.0;
		
	vec4 compareColor = inputColor;
	compareColor.r -= 0.01;
	compareColor.g -= 0.01;
	compareColor.b += 0.01;
	
	// color.r > 0.6 && color.g > 0.6 && color.b < 0.45
	//if( sumColor.r > inputColor.r && sumColor.g > inputColor.g && sumColor.b > inputColor.b) //  || sumColor.b > 0.9
	
	if( sumColor.r >= compareColor.r && sumColor.g >= compareColor.g && sumColor.b <= compareColor.b && sumColor.b > 0.3)
	{
		gl_FragColor = vec4(1,0,0,1);
	}
	else
	{
		//gl_FragColor = vec4(1,1,0,1);
		gl_FragColor = texture2D(videoFrame, gl_TexCoord[0].st);
	}
 }
*/